<?php
/**
 * Created by PhpStorm.
 * User: huliren<huliren0516@163.com>
 * Date: 2017/9/25
 * Time: 15:52
 */
require('../../../common.inc.php');
define('SITE_AUTH', 'activity-manage-qrcode');

use lib\core\AdminAction;
use Act\SignQrcode as Model;
use Common\Upload;
use Common\ResourcePathManager;

class signqrcode extends AdminAction
{
    private $list_url = 'data/modules/manage/signqrcode.php';

    public function doDefault()
    {
        $smarty = $this->app->page();

        $where = ['Id >' => 0];

        $page = $_GET['page'] < 1 ? 1 : (int)$_GET['page'];
        $limit = parent::PAGE_SIZE;
        $offset = ($page - 1) * $limit;

        $info = Model::getList($where, $limit, $offset);
        $count = $info['count'];

        if ($count > 0) {
            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;

            $pagination = $this->pagination($count, $limit, $page);
            $smarty->value('page_info', $page_info);
            $smarty->value('pagination', $pagination);
            $smarty->value('list', $info['data']);
        }

        $smarty->output('manage/signqrcode_list.tpl');
    }

    public function doEdit()
    {
        $smarty = $this->app->page();
        $id = intval(trim($_GET['id']));

        if ($id < 0) {
            HDshowMsg('数据不存在！', ['type' => 'error', 'url' => $this->list_url]);
        }
        if (!empty($id)) {
            $data = Model::getInfoByIds($id);
            if (empty($data)) {
                HDshowMsg('数据不存在！', ['type' => 'error', 'url' => $this->list_url]);
            }
            $smarty->value('info', $data);
        }
        $smarty->output('manage/signqrcode_edit.tpl');
    }

    public function doUpdata()
    {
        $id = intval(trim($_POST['id']));
        $name = empty(trim($_POST['name'])) ? '' : trim($_POST['name']);
        $imgurl = $_FILES['img'];
        $limitcount = intval($_POST['limitcount']);

        $imgUrlAddre = '';
        if (!empty($id)) {
            $data = [
                Model::FIELD_NAME=>$name,
                Model::FIELD_LIMIT_COUNT=>$limitcount
            ];

            if (isset($imgurl['name']) && $imgurl['error'] == 0 && $imgurl['size'] > 0) {
                $imgUrlAddre = Upload::saveFile($imgurl['tmp_name'],ResourcePathManager::getSignQrcodeSavePath());
                $data = array_merge($data, [Model::FIELD_IMG_URL => $imgUrlAddre]);
            }

            $rel = Model::update($id,$data);

        } else {
            if (isset($imgurl['name']) && $imgurl['error'] == 0 && $imgurl['size'] > 0) {
                $imgUrlAddre = Upload::saveFile($imgurl['tmp_name'],ResourcePathManager::getSignQrcodeSavePath());
            }else{
                HDshowMsg('img图片上传失败！', ['type' => 'error', 'url' => $this->list_url]);
            }

            $data = [
              Model::FIELD_NAME=>$name,
              Model::FIELD_IMG_URL=>$imgUrlAddre,
              Model::FIELD_LIMIT_COUNT=>$limitcount
            ];

            $rel = Model::add($data);
        }


        if ($rel) {
            HDshowMsg('保存成功', ['type' => 'success', 'url' => $this->list_url]);
        } else {
            HDshowMsg('操作失败', ['type' => 'error', 'url' => $this->list_url]);
        }
    }

    public function doDelete(){
        $id = $_GET['id'];
        $rel = Model::delete($id);
        if ($rel) {
            HDshowMsg('删除成功', ['type' => 'success', 'url' => $this->list_url]);
        } else {
            HDshowMsg('操作失败', ['type' => 'error', 'url' => $this->list_url]);
        }
    }
}
$app->run();